package cn.hyy.FVOnline.controller;


import cn.hyy.FVOnline.entity.Address;
import cn.hyy.FVOnline.entity.PageEntitys;
import cn.hyy.FVOnline.service.AddressService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.hyy.FVOnline.service.GoodsService;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/che")
public class CheController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/addChe",produces = "text/html;charset=UTF-8;")
    public @ResponseBody String addChe(@RequestParam("userId") String userId,@RequestParam("goodsid") String goodsid, @RequestParam("num") String num,HttpSession session){
        JSONObject jo = new JSONObject();
        try {
            Object o = session.getAttribute(userId);
            if (o == null){
                List<String> list = new ArrayList<>();
                list.add(goodsid+"_"+num);
                session.setAttribute(userId,list);
            }else {
                List<String> list = (List<String>) o;
                list.add(goodsid+"_"+num);
                session.setAttribute(userId,list);
            }
            jo.put("code","success");
            jo.put("msg","加入购物车成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("msg","加入购物车失败");
        }
        return jo.toJSONString();
    }

    @Autowired
    private AddressService addressService;

    @RequestMapping(value = "/getCar",produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getCar(@RequestParam("userId") String userId,HttpSession session){

        Object o = session.getAttribute(userId);
        PageEntitys car = null;
        if (o != null){
            List<String> lists = (List<String>) o;
            car = goodsService.getCar(lists);
            List<Address> address = addressService.findMyAddress(Integer.parseInt(userId));
            car.setAddresses(address);
        }
        return JSONObject.toJSONString(car);
    }



    @RequestMapping(value = "/removegoods",produces = "text/html;charset=UTF-8;")
    public @ResponseBody String removeGoods(@RequestParam("userId") String userId,@RequestParam("goodsid") String goodsid, @RequestParam("num") String num,HttpSession session){

        JSONObject jo = new JSONObject();

        try {

            Object o = session.getAttribute(userId);
            List<String> list = (List<String>) o;
            list.remove(goodsid+"_"+num);
            session.setAttribute(userId,list);
            jo.put("code","success");
            jo.put("msg","移出购物车成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("msg","移出购物车失败");
        }

        return jo.toJSONString();
    }


}
